package com.openatc.autoprogram.thread;

import com.google.gson.Gson;
import com.openatc.autoprogram.model.DevCover;
import com.openatc.autoprogram.model.MessageData;
import com.openatc.autoprogram.service.MsgService;
import com.openatc.autoprogram.utils.ApplicationContextProvider;

import java.util.Calendar;
import java.util.Date;

/**
 * @Classname AutoSendMsgThread
 * @Description
 * @Date 2022/10/18 16:52
 * @Created by panleilei
 */
public class AutoSendMsgThread extends Thread {
    private MsgService msgService = ApplicationContextProvider.getBean(MsgService.class);
    private static final String LOGIN = "login";
    private static final String REPORT = "report";
    private static final String STATUS_PATTERN = "status/pattern";
    private static final String PLATFORM = "SCATS";
    private static final String PROTOCOL = "scp";
    private DevCover devCover = new DevCover();

    private String ip;
    private int port;
    private String agentid;
    private Gson gson = new Gson();

    public AutoSendMsgThread(String ip, int port,String agentid){
        this.ip = ip;
        this.port = port;
        this.agentid = agentid;
    }

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        Date time = cal.getTime();
        System.out.println(time);
    }



    @Override
    public void run() {
        while (true) {
            sendLoginMsg();
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }



    private void sendLoginMsg() {
        MessageData loginReport = new MessageData();
        loginReport.setAgentid(agentid);
        loginReport.setInfotype(LOGIN);
//        loginReport.setModel();
        loginReport.setOperation(REPORT);
        loginReport.setThirdpartyid(agentid);

        devCover.setPlatform(PLATFORM);
        devCover.setProtocol(PROTOCOL);
        devCover.setAgentid(agentid);
        loginReport.setData(gson.toJsonTree(devCover));
        msgService.sendUdpMsgToOpenATC(loginReport);

    }
}


